package com.fudan.sys.security;

import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ShiroConfiguration {

	@Bean
	@Autowired
	public Realm realm() {
		SysRealm sysRealm = new SysRealm();
		sysRealm.setCredentialsMatcher(credentialsMatcher());
		return sysRealm;
	}


	public HashedCredentialsMatcher credentialsMatcher() {
		HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher("MD5");
		credentialsMatcher.setHashIterations(2);
		return credentialsMatcher;
	}

	@Bean
	@Autowired
    public ShiroFilterChainDefinition shiroFilterChainDefinition() {
        DefaultShiroFilterChainDefinition chain = new DefaultShiroFilterChainDefinition();

        // shiro 提供的登出过滤器，访问指定的请求，就会执行登录，默认跳转路径是"/"，或者是"shiro.loginUrl"配置的内容
        // 由于application-shiro.yml中配置了 shiro:loginUrl: /page/401，返回会返回对应的json内容
        // 可以结合/user/login和/t1/js接口来测试这个/t4/logout接口是否有效
        chain.addPathDefinition("/sys/login", "anon");
        chain.addPathDefinition("/sys/logout","logout");
        chain.addPathDefinition("/static/**","anon");
        chain.addPathDefinition("/resource/**","anon");
        //其它路径均需要登录
        chain.addPathDefinition("/**", "user");

        return chain;
    }

	  @Bean
	  @Autowired
	  public static DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() {
	      DefaultAdvisorAutoProxyCreator creator = new DefaultAdvisorAutoProxyCreator();
	      creator.setProxyTargetClass(true);
	      return creator;
	  }
//	@Bean
//    public SessionsSecurityManager securityManager() {
//        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
//        securityManager.setRealm(realm());
//        return securityManager;
//    }
//

//
//    @Bean
//    public ShiroFilterFactoryBean shiroFilterFactoryBean(org.apache.shiro.mgt.SecurityManager securityManager) {
//        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
//        shiroFilterFactoryBean.setSecurityManager(securityManager);
//        Map<String,String> map = new HashMap<String, String>();
//        //登出
//        map.put("/logout","logout");
//        map.put("/static/**","anon");
//        map.put("/resource/**","anon");
//        map.put("/sys/login", "anon");
//        //对所有用户认证
//        map.put("/**","user");
//        //登录
//        shiroFilterFactoryBean.setLoginUrl("/login");
//        //首页
//        shiroFilterFactoryBean.setSuccessUrl("/index");
//        //错误页面，认证不通过跳转
//        shiroFilterFactoryBean.setUnauthorizedUrl("/error");
//        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
//        return shiroFilterFactoryBean;
//    }


}
